﻿using B.S.BaseData.Api.Read.Command.HospitalCommand;
using B.S.BaseData.ErrorCode;
using B.S.BaseData.Infrastructure.Interface;
using B.S.BataBase.Domain.AI;
using B.S.BataBase.Domain.DTO.GoodsDTO;
using MediatR;

namespace B.S.BaseData.Api.Read.CommandHandler.HospitalCommandHandler
{
    public class PatientShowCommandHandler : IRequestHandler<PatientShowCommand, APIResult<ApiPaging<PatientModel>>>
    {
        private readonly IBaseRepository<PatientModel> _baseRepository;
        public PatientShowCommandHandler(IBaseRepository<PatientModel> baseRepository)
        {
            _baseRepository = baseRepository;
        }
        public Task<APIResult<ApiPaging<PatientModel>>> Handle(PatientShowCommand request, CancellationToken cancellationToken)
        {
            APIResult<ApiPaging<PatientModel>> result = new APIResult<ApiPaging<PatientModel>>();
            var list = _baseRepository.GetAll().Where(x => x.IsDel == false); ;
            if (request.Id != null)
            {
                list = list.Where(x => x.Id == request.Id);
            }
            var totaCount = list.Count();
            var pageCount = (int)Math.Ceiling(totaCount * 1.0 / request.pageSize);
            ApiPaging<PatientModel> paging = new ApiPaging<PatientModel>();
            paging.TotalPage = pageCount;
            paging.Total = totaCount;
            paging.PageData = list.Skip((request.pageIndex - 1) * request.pageSize).Take(request.pageSize).ToList();
            result.Data = paging;
            result.Code = ApiEnum.Success;
            result.Msg = "患者信息显示成功";
            return Task.FromResult(result);
        }
    }
}
